#include <stdio.h>
#include <stdlib.h>

#include "LinkedList.h"

//----------------------------------------------- node 
//----------------------------------------------- node 
//----------------------------------------------- node 
int IDS = 0;
LNode *newNode(int data) {
	LNode *node = malloc(sizeof(LNode));
	node->id = IDS;
	node->data = data;
	node->next = NULL;
	node->prev = NULL;

	IDS++;

	return node;	
}
int hasNext(LNode *node) {
	if(node->next==NULL) return 0;
	else return 1;
}
int hasPrev(LNode *node) {
	if(node->prev==NULL) return 0;
	else return 1;
}
void dumpNode(LNode *node) {
	printf("dumpNode >  ");
	if(node==NULL) {
		printf(" NULL node\n");
	}
	else {
		printf("id: %d, data: %d, ", node->id, node->data);
		// next
		if (hasNext(node)==1)
			printf("->next: %d ",node->next->id);
		else 
			printf("->next: NULL ");
		// prev
		if (hasPrev(node)==1) 
			printf("->prev: %d ", node->prev->id);
		else
			printf("->prev: NULL");
	}
	printf("\n");
}
void deleteNode(LNode *node) {
	free(node);
}
void setNodeData(LNode *node, int n) {
	node->data = n;
}

//----------------------------------------------- list
//----------------------------------------------- list 
//----------------------------------------------- list 
List *newList(LNode *head) {
	List *l = malloc(sizeof(List));
	l->head = head;
	l->tail = head;
	l->size = 1;
	return l;
}
LNode *getHead(List *l) {
	return l->head;
}
LNode *getTail(List *l) {
	return l->tail;
}
void addInHead(List *l, LNode *n) {
	l->head->prev = n;
	n->next = l->head;
	l->head = n;
	l->size++;
}
void addinTail(List *l, LNode *n) {
	n->prev = l->tail;
	l->tail->next = n;
	l->tail = n;
	l->size++;	
}
LNode *removeTail(List *l) {
	LNode *oldTail = l->tail;
	l->tail = oldTail->prev;
	l->tail->next = NULL;
	l->size--;
	return oldTail;
}
LNode *removeHead(List *l) {
	LNode *oldHead = l->head;
	l->head = oldHead->next;
	l->head->prev = NULL;
	l->size--;
	return oldHead;
}
void dumpHeadTail(List *l) {
	printf("\n");
	printf("head: "); dumpNode(getHead(l));
	printf("tail: "); dumpNode(getTail(l));
	printf("\n");
}

//----------------------------------------------- list
//----------------------------------------------- list 
//----------------------------------------------- list 
LIterator *newIterator(List *l) {
	LIterator *itr = malloc(sizeof(LIterator));
	itr->list = l;
	//itr->current = l->head;
	return itr;
}
int moveNext(LIterator *itr) {
	if(hasNext(itr->current)==1) {
		itr->current = itr->current->next;
		return 1;
	} else return 0;
}
int movePrev(LIterator *itr) {
	if(hasPrev(itr->current)==1) {
		itr->current = itr->current->prev;
		return 1;
	} else return 0;
}
void dumpTraverse(LIterator *itr) {
	itr->current = itr->list->head;
	do {
		dumpNode(itr->current);
	} while(moveNext(itr)==1);
}
void deleteLIterator(LIterator *itr) {
	free(itr);
}

//---------------------------------------- delete list using iterator
void deleteList(List *l) {
	LIterator *itr = newIterator(l);
	itr->current = l->head;
	do {
		deleteNode(itr->current->prev);
	} while(moveNext(itr)==1);
	deleteLIterator(itr);
	free(l);
}




